package com.xdhl.fu.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xdhl.fu.common.core.constant.SecurityConstants;
import com.xdhl.fu.common.core.web.page.PageDomain;
import com.xdhl.fu.common.core.web.page.TableSupport;
import com.xdhl.fu.common.security.utils.SecurityUtils;
import com.xdhl.fu.flight.info.api.RemoteFlightInfoService;
import com.xdhl.fu.flight.info.api.mongodb.domain.MGReservation;
import com.xdhl.fu.member.api.RemoteMemberBasicService;
import com.xdhl.fu.order.domain.ChangeBody;
import com.xdhl.fu.order.domain.OrderInfo;
import com.xdhl.fu.order.domain.RefundBody;
import com.xdhl.fu.order.domain.vo.OrderInfoVO;
import com.xdhl.fu.order.mapper.OrderInfoMapper;
import com.xdhl.fu.order.repository.MGReservationRepository;
import com.xdhl.fu.order.service.IOrderInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单信息
 * Service业务层处理
 *
 * @author 朱力维
 * @time 2022/5/29 17:01
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    RemoteMemberBasicService remoteMemberBasicService;

    @Resource
    RemoteFlightInfoService remoteFlightInfoService;

    @Resource
    private MGReservationRepository mgReservationRepository;


    @Override
    public List<OrderInfoVO> countHotCityTop16() {
        List<OrderInfoVO> orderInfoVOS = new ArrayList<>();
        return orderInfoVOS;
    }

    @Override
    public Page<MGReservation> listMemberOrder() {
        String mDataId = remoteMemberBasicService.getDataIdSelf(SecurityUtils.getToken(), SecurityConstants.INNER).getData();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Page<MGReservation> reservations = mgReservationRepository.findByMemberDataIdEquals(mDataId, PageRequest.of(--pageNum, pageSize));
        return reservations;
    }

    @Override
    public List<OrderInfo> listMemberChangeOrder() {
        String mDataId = remoteMemberBasicService.getDataIdSelf(SecurityUtils.getToken(), SecurityConstants.INNER).getData();
        LambdaQueryWrapper<OrderInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderInfo::getMemberDataId, mDataId);
        lqw.eq(OrderInfo::getChangeStatus, "B");
        List<OrderInfo> list = list(lqw);
        return list;
    }

    @Override
    public List<OrderInfo> listUnpaid() {
        String mDataId = remoteMemberBasicService.getDataIdSelf(SecurityUtils.getToken(), SecurityConstants.INNER).getData();
        LambdaQueryWrapper<OrderInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderInfo::getMemberDataId, mDataId);
        lqw.eq(OrderInfo::getOStatus, "A");
        List<OrderInfo> list = list(lqw);
        return list;
    }

    @Override
    public boolean refund(RefundBody refundBody) {
        OrderInfo orderInfo = getById(refundBody.getDataId());
        MGReservation reservation = mgReservationRepository.findById(orderInfo.getSerialNumber()).get();
        // TODO 四折以下机票无法申请退款

        // 距离起飞时间小于1个小时无法申请退款
        LocalDateTime startTime = reservation.getFlightSchedule().getFlightSchedule().getStartTime();
        Duration duration = Duration.between(LocalDateTime.now(), startTime);
        if (duration.toHours() < 1) {
            return false;
        }
        // 已经起飞的无法申请退款
        if (startTime.isAfter(LocalDateTime.now())) {
            return false;
        }
        orderInfo.setOStatus("C");
        orderInfo.setChargebackStatus("A");
        orderInfo.setChargebackTime(LocalDateTime.now());
        orderInfo.setUpdateTime(LocalDateTime.now());
        // 订单退款理由
        orderInfo.setRemark(refundBody.getRemark());
        reservation.setOrder(JSONObject.parseObject(JSONObject.toJSONString(orderInfo)));
        mgReservationRepository.save(reservation);
        updateById(orderInfo);
        return true;
    }

    @Override
    public boolean change(ChangeBody changeBody) {
        OrderInfo orderInfo = getById(changeBody.getOrderDataId());
        MGReservation reservation = mgReservationRepository.findById(orderInfo.getSerialNumber()).get();
        orderInfo.setChangeStatus("B");
        orderInfo.setChangeTime(LocalDateTime.now());
        orderInfo.setUpdateTime(LocalDateTime.now());
        orderInfo.setChangeDepartureTime(changeBody.getStartTime());
        orderInfo.setChangeArrivalTime(changeBody.getEndTime());
        // 订单改期理由
        orderInfo.setRemark(changeBody.getRemark());
        reservation.setOrder(JSONObject.parseObject(JSONObject.toJSONString(orderInfo)));
        mgReservationRepository.save(reservation);
        updateById(orderInfo);
        return true;
    }
}
